#include "zbTime.h"
__IO uint32_t TimingDelay;				// for delay timer

void initTime(void)
{
	// generate int every 1ms for timing stuff
	systickSetup();
}


/**
  * @brief  
  *        
  *         
  * @note   T
  *			
  *			
  * @param  none
  * @retval none
  */
u32 getMSTick()
{
	return mygVars->milliseconds;
}
/**
  * @brief  
  *        
  *         
  * @note   T
  *			
  *			
  * @param  none
  * @retval none
  */
u32 getSecondTick()
{
	return mygVars->seconds;
}

void setSecondTick(u32 newSec)
{
	mygVars->seconds = newSec;
}


/**
  * @brief 
  *       
  *         
  * @note   
  * @param  none
  * @retval none
  */
void systickSetup(void)
{
	if (SysTick_Config(SystemCoreClock / 1000))
	{ 
	/* Capture error */ 
		while (1);
	}
}

/**
  * @brief  Inserts a delay time.
  * @param  nTime: specifies the delay time length, in milliseconds.
  * @retval None
  */
void Delay(__IO uint32_t nTime)
{ 
  TimingDelay = nTime;

  while(TimingDelay != 0);
}

/**
  * @brief  Decrements the TimingDelay variable.
  * @param  None
  * @retval None
  */
void TimingDelay_Decrement(void)
{
  if (TimingDelay != 0x00)
  { 
    TimingDelay--;
  }
}

/**
  * @brief int on sysclk, every ms
  *       
  *         
  * @note   
  * @param  none
  * @retval none
  */
void SysTick_Handler(void)
{
	TimingDelay_Decrement();
	mygVars->milliseconds++;

	// every 100 milliseconds incrament the seconds
	if(mygVars->milliseconds % 1000 == 0)
	{
		mygVars->seconds++;
	}
}
